package com.landinfo.service.mapper;

import com.landinfo.domain.*;
import com.landinfo.service.dto.SampleLineMonitorDTO;

import com.vividsolutions.jts.geom.Coordinate;
import com.vividsolutions.jts.geom.LineString;
import org.mapstruct.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Mapper for the entity SampleLineMonitor and its DTO SampleLineMonitorDTO.
 */
@Service
public class SampleLineMonitorMapper {

    public SampleLineMonitor fromId(Long id) {
        if (id == null) {
            return null;
        }
        SampleLineMonitor sampleLineMonitor = new SampleLineMonitor();
        sampleLineMonitor.setId(id);
        return sampleLineMonitor;
    }

    public SampleLineMonitor toEntity(SampleLineMonitorDTO dto) {
        if ( dto == null ) {
            return null;
        }

        SampleLineMonitor sampleLineMonitor = new SampleLineMonitor();

        sampleLineMonitor.setId( dto.getId() );
        sampleLineMonitor.setName( dto.getName() );
        sampleLineMonitor.setStartPlaceName( dto.getStartPlaceName() );
        sampleLineMonitor.setEndPlaceName( dto.getEndPlaceName() );
        sampleLineMonitor.setMonitorPersons( dto.getMonitorPersons() );
        sampleLineMonitor.setDistance( dto.getDistance() );
        sampleLineMonitor.setEnable( dto.isEnable() );
        sampleLineMonitor.setGeometry( null );
        sampleLineMonitor.setSort( dto.getSort() );

        return sampleLineMonitor;
    }

    public SampleLineMonitorDTO toDto(SampleLineMonitor entity) {
        if ( entity == null ) {
            return null;
        }

        SampleLineMonitorDTO sampleLineMonitorDTO = new SampleLineMonitorDTO();

        sampleLineMonitorDTO.setId( entity.getId() );
        sampleLineMonitorDTO.setName( entity.getName() );
        sampleLineMonitorDTO.setStartPlaceName( entity.getStartPlaceName() );
        sampleLineMonitorDTO.setEndPlaceName( entity.getEndPlaceName() );
        sampleLineMonitorDTO.setMonitorPersons( entity.getMonitorPersons() );
        sampleLineMonitorDTO.setDistance( entity.getDistance() );
        sampleLineMonitorDTO.setEnable( entity.isEnable() );
        sampleLineMonitorDTO.setSort( entity.getSort() );
        LineString lineString = entity.getGeometry();
        Coordinate[] list1 = lineString.getCoordinates();
        List<Double[]> list = new ArrayList<>();
        for (int i = 0; i < list1.length; i++) {
            list.add(new Double[]{list1[i].x, list1[i].y});
//            // 过滤掉最后一个点，因为最后一个点和第一个点时相同的
//            if(i < (list1.length -1)){
//                list.add(new Double[]{list1[i].x, list1[i].y});
//            }
        }
        sampleLineMonitorDTO.setGeometry( list );

        return sampleLineMonitorDTO;
    }
}
